home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_005 / proctest / proctest.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  178 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* system software version: V1.1               */
  31.  
  32. #include "exec/types.h"
  33. #include "exec/nodes.h"
  34. #include "exec/lists.h"
  35. #include "exec/libraries.h"
  36. #include "exec/ports.h"
  37. #include "exec/interrupts.h"
  38. #include "exec/io.h"
  39. #include "libraries/dos.h"
  40. #include "libraries/dosextens.h"
  41.  
  42. #include "workbench/startup.h"
  43.  
  44. #define PRIORITY 0
  45. #define STACKSIZE 5000
  46.  
  47. extern struct Message *GetMsg();
  48. extern int LoadSeg();
  49. extern struct MsgPort *CreateProc();
  50.  
  51. struct mymess { 
  52.    struct Message sysmess;
  53.    int outpointer;
  54.    int errpointer; 
  55. };
  56.  
  57. struct mymess pmsg;
  58. struct WBStartup wbm;    
  59.  
  60. main()
  61. {
  62.     struct WBStartup *msg;        /* message that we send to the
  63.                      * process to wake it up */
  64.     struct Message *reply;
  65.     struct Process *myprocess;
  66.  
  67.     struct mymess *parms;    /* message to contain my own parameters
  68.                  * to pass on to spawned process, sample
  69.                  * only.  Just to prove that we correctly
  70.                  * create a process, we are giving it
  71.                  * something other than nil: as its
  72.                  * stdout and stderr... in fact, giving
  73.                  * it OUR values so we can share the
  74.                  * same output window.
  75.                  */
  76.  
  77.     /* The WBStartup message could be dynamically allocated or statically
  78.        * as it is done in this case.  Because of the static allocation,
  79.      * poor main() has to go to sleep forever after starting up
  80.      * the daughter process, waiting for a message to arrive at
  81.      * its message port before it can actually exit.
  82.      */
  83.  
  84.     /* because main() is itself started as a process, it automatically
  85.      * has a message port allocated for itself.  Located at
  86.      *  &((struct Process *)FindTask(0))->pr_MsgPort 
  87.      */
  88.  
  89.     int littleSeg;
  90.  
  91.     struct MsgPort *mainport;    /* pointer to main's msg port */
  92.     struct MsgPort *littleProc;     /* pointer to daughter's msg port */
  93.  
  94. /* LOAD THE PROGRAM TO BE STARTED FROM MAIN ****************************** */
  95.  
  96.     littleSeg = LoadSeg("littleproc");
  97.     if(littleSeg == 0) 
  98.     {
  99.         printf("\nFile not found");
  100.         exit(999);
  101.     }
  102.  
  103. /* CREATE A PROCESS FOR THIS OTHER PROGRAM ******************************* */
  104.  
  105.     littleProc = CreateProc("littleguy",PRIORITY, littleSeg, STACKSIZE);
  106.     if( littleProc == 0 ) 
  107.     {
  108.         printf("\Couldn't create the process");
  109.         UnLoadSeg(littleSeg);
  110.         exit( 1000 );
  111.     }
  112.  
  113. /* ********************************************************************** */
  114. /* locate the message port that is allocated as part of the process
  115.    that started main() in the first place */
  116.  
  117.     myprocess = (struct Process *)FindTask(0);
  118.  
  119.     mainport = &myprocess->pr_MsgPort;
  120.  
  121. /* ********************************************************************** */
  122. /* THIS CODE BLOCK STARTS THE PROCESS RUNNING, AS THOUGH CALLED FROM WBENCH */
  123.  
  124.     /* this message block is a wakeup call to the process we created. */
  125.     msg = &wbm;
  126.  
  127.     /* preset the necessary arguments for message passing */
  128.  
  129.     msg->sm_Message.mn_ReplyPort = mainport;
  130.     msg->sm_Message.mn_Length = sizeof(struct WBStartup);
  131.  
  132.     /* passing no workbench arguments to this process; we are not WBench */
  133.  
  134.     msg->sm_ArgList = NULL;
  135.  
  136.     /* if the process is being opened without a ToolWindow (Workbench
  137.      * sets this up) as a parent, we should simply go on to do main() */
  138.  
  139.     msg->sm_ToolWindow = NULL;
  140.  
  141.     PutMsg(littleProc,msg);    /* send the startup message */
  142.  
  143. /* ************************************************************************ */
  144. /* just a sample message, still using the same message and reply ports      */
  145.  
  146.     parms = &pmsg;
  147.     parms->sysmess.mn_ReplyPort = mainport;
  148.     parms->sysmess.mn_Length = sizeof(struct mymess);
  149.     parms->outpointer = Output();
  150.     parms->errpointer = Output();
  151.  
  152.     PutMsg(littleProc,parms);    /* send him our parameters */
  153.  
  154.     WaitPort(mainport);    /* wait for the reply from parameter pass. */
  155.     reply = GetMsg(mainport); /* value should be "parms" if checked */    
  156.  
  157.  
  158.  
  159.     WaitPort(mainport);    /* wait for the return of the wbstartup
  160.                    * message before main itself is allowed
  161.                  * to exit.  */
  162.  
  163.     reply = GetMsg(mainport); /* value should be "msg" if checked */
  164.  
  165.     /* NOTE: there should be checking here to see if the message
  166.      * received at this port was the string, or the wakeup call.
  167.      * This sample code only assumes that the string is received
  168.      * and replied first, then the wakeup call message is returned
  169.      * as the little task is exiting.  
  170.      */
  171.  
  172.     UnLoadSeg(littleSeg);
  173.  
  174.     printf("\nSlave process exited, master unloaded its code/data");
  175.  
  176. }    /* end of main */
  177.  
  178.